package com.tomatozq;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.tomatozq.dao.SysLogDynamicSqlSupport;
import com.tomatozq.dao.SysLogMapper;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.select.SelectDSL;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Date;

import static org.mybatis.dynamic.sql.SqlBuilder.count;
import static org.mybatis.dynamic.sql.SqlBuilder.isBetween;

@SpringBootApplication
public class MybatisMbgApplication implements CommandLineRunner {
	@Autowired
	private SysLogMapper sysLogMapper;

	@Override
	public void run(String... args) throws Exception {
		Date startDate = DateUtil.parse("2019-10-01");
		Date endDate = DateUtil.parse("2019-10-26");
//		SysLogExample example = new SysLogExample();
//		example.createCriteria()
//				.andRecordTimeBetween(
//						DateUtil.parse("2019-10-01"),
//						DateUtil.parse("2019-10-25"));
//
//		long count = sysLogMapper.countByExample(example);
//
//		System.out.println(count);
		SelectStatementProvider selectStatement = SelectDSL.select(count())
				.from(SysLogDynamicSqlSupport.sysLog)
				.where(SysLogDynamicSqlSupport.recordTime,
						isBetween(startDate).and(endDate))
				.build()
				.render(RenderingStrategies.MYBATIS3);

		System.out.println(selectStatement.toString());

		System.out.println(sysLogMapper.count(selectStatement));
	}

	public static void main(String[] args){
		SpringApplication.run(MybatisMbgApplication.class, args);
	}
}

